<template>
    <div>
        <el-form :label-position="labelPosition" label-width="80px" :rules="rules" :model="registerForm" ref="registerForm" class="Loginbox">
            <h3>用户注册</h3>
            <el-row>
                <router-link to="/user/login" class="gologin">返回登录</router-link>
            </el-row>
            <el-form-item label="账号">
                <el-input v-model="registerForm.username"  placeholder="请输入账号" clearable maxlength="20" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input v-model="registerForm.password" placeholder="请输入密码" show-password clearable maxlength="20" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPass">
                <el-input v-model="registerForm.checkPass" placeholder="请再次输入密码" show-password clearable maxlength="20" show-word-limit></el-input>
            </el-form-item>
            <el-row>
                <el-button type="primary" style="width:100%" @click="register('registerForm')">注册</el-button>
            </el-row>
            
        </el-form>

    </div>
</template>

<script>
  export default {
    data() {

    var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.registerForm.checkPass !== '') {
            this.$refs.registerForm.validateField('checkPass');
          }
          callback();
        }
      };  
    var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.registerForm.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };    

      return {
        labelPosition: 'right',
        registerForm: {
          username: "",
          password: "",
          checkPass:"",
        
        },
        rules:{
          password: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
        }
      };
    },
    methods:{

      register( formName ){

        this.$refs[formName].validate((valid) => {
          if (valid) {
           //注册请求 
            this.$http.post('register', { 
            "username":this.registerForm.username,
            "password":this.registerForm.password,
        
          }).then( res =>{
            //服务器正常响应的信息
              console.log( res)
            //提示成功
              this.$message({
                    message: '账号创建成功',
                    type: 'success'
                  });
            //跳转到注册页面    
            this.$router.push('/user/login') 
          }).catch( (res )=>{
            //未成功注册
            console.log(res)
            //服务器异常
            if(res.code == 'ERR_NETWORK'){
                this.$message.error('注册失败，服务器未响应');
              return;
            }else if(res.code == 'ERR_BAD_REQUEST'){
            //console.log(res.response.data)
            //页面提示错误信息
            this.$message.error(res.response.data);
            //console.log(res)
            return;
          }else if(res.code == 'ERR_BAD_RESPONSE'){
            //console.log(res.response.data)
            //页面提示错误信息
            this.$message.error('注册失败，服务器处理异常');
            //console.log(res)
            return;
          }
        })
            
          } else {
            //这是饿了么UI的提示
            this.$message.error('请正确输入所有信息');
            console.log('error submit!!');
            return false;
          }
        });
        console.log( this.registerForm)
        
      }
    }
  }
</script>

<style scoped>
.Loginbox{
    border-radius: 15px;
    background-clip: padding-box;
    margin: 180px auto;
    width: 350px;
    padding: 15px 35px 15px 35px;
    background-color: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
}
.gologin{
    display: inline-block;
    text-decoration: none;
    margin-top: -20px;
    margin-bottom: 20px;
    display: flex;
    flex-direction:row-reverse;
    color:rgb(0, 129, 204)
}
</style>